<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'
export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        id: {
          hidden: true
        },
        dayTime: {
          label: '生产日期',
          type: 'date',
          placeholder: '请选择生产日期'
        },
        productionState:{
          label: '换产状态',
          type: 'radio',
          radioBorder: true,
          radioList: [
            {text: '上机', label: 0},
            {text: '继续生产', label: 5},
            {text: '下机', label: 10}
          ],
          default: 0
        },
        orderDetailId:{
          label: '订单编号',
          type: 'list',
          list: [],
          placeholder: '请输入订单编号',
          selectCallback: this.orderSelectCallback
        },
        deviceId: {
          label: '设备',
          list: [],
          type: 'list',
          placeholder: '请选择设备'
        },
        classId: {
          label: '班别',
          list: [],
          type: 'list',
          placeholder: '请输入班别'
        },
        userId: {
          label: '员工编号',
          list: [],
          type: 'list',
          placeholder: '请输入员工编号'
        },
        productId: {
          label: '产品',
          type: 'list',
          list: [],
          disabled: true,
          placeholder: '请输入产品'
        },
        boxPieceNumber: {
          label: '箱只',
          type: 'number',
          max: 10000,
          default: 0,
          selectCallback: this.boxPieceCallback
        },
        boxNumber: {
          label: '箱数',
          type: 'number',
          max: 10000,
          default: 0,
          selectCallback: this.boxPieceCallback
        },
        scrapNumber: {
          label: '零头',
          type: 'number',
          max: 10000,
          default: 0,
          selectCallback: this.boxPieceCallback
        },
        productNumber: {
          label: '总只数',
          default: 0
        },
        productWeight: {
          label: '订单单重',
          type: 'number',
          max: 1000,
          default: 0
        },
        actualProductWeight: {
          label: '生产单重',
          type: 'number',
          max: 1000,
          default: 0
        },

        actualCountWeight: {
          label: '总重量（g）',
          disabled: true,
          default: 0
        },
        machineSpeed: {
          label: '机速',
          placeholder: '机速',
          type: 'number',
          max: 1000,
          default: 0
        },
        timerNumber: {
          label: '时长',
          placeholder: '时长',
          type: 'number',
          max: 1000,
          default: 0
        },
        successRate: {
          label: '成品率',
          disabled: true,
          default: 0
        },

        // status: {
        //   label: '状态',
        //   type: 'switch',
        //   activeValue: 1,
        //   inactiveValue: 0,
        //   default: 1
        // }
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.initProductDeviceList()
      this.initUserInfoList()
      this.initProductInfoList()
      this.initClassInfoList()
      this.initOrderDetailList()

      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule('deviceId', 'classId', 'userId', 'productId', 'orderDetailId','boxPieceNumber', 'boxNumber', 'productWeight', 'dripLiquid', 'dayTime', 'status'),
        initApi: 'dayApi/dayProductCountInfo',
        saveApi: 'dayApi/saveDayProductCount',
        updateApi: 'dayApi/updateDayProductCount',
        objectName: 'dayProductCount'
      })

      this.$nextTick(() => {
        const dataForm = this.dataForm

        Object.defineProperty(dataForm, 'actualCountWeight', {
          get() {
            return dataForm.productNumber * dataForm.actualProductWeight
          }
        })
        Object.defineProperty(dataForm, 'successRate', {

          get() {
            if (0!==dataForm.dripLiquid) {
              return (dataForm.productNumber / (dataForm.machineSpeed * 60 * dataForm.timerNumber) * 100).toFixed(2)
            }else {
              return 0
            }
          }
        })

      })
    },
    orderSelectCallback() {
      this.orderSelectHandle(this.dataForm.orderDetailId, (data) => {
        this.dataForm['productId'] = this.R.pathOr(0, ['orderDetail', 'productId'], data)
        this.dataForm['productWeight'] = this.R.pathOr(0, ['orderDetail', 'productWeight'], data)
      })
    },
    orderSelectHandle(orderId, cb) {
      if (this.R.isNil(orderId) || this.R.isEmpty(orderId)) {
        return
      }
      mRequest('orderApi/orderDetailInfo', {id: orderId}, ({data}) => {
        if (!(cb instanceof Function)) {
          return
        }
        return cb(data)
      })
    },
    boxPieceCallback(){
      const boxPieceNumber = this.dataForm.boxPieceNumber
      const boxNumber = this.dataForm.boxNumber
      const scrapNumber = this.dataForm.scrapNumber
      this.dataForm['productNumber'] = (boxPieceNumber * boxNumber) + scrapNumber
      this.dataForm.successRate = (this.dataForm.productNumber / (this.dataForm.machineSpeed * 60 * this.dataForm.timerNumber) * 100).toFixed(2)
    }
  }
}
</script>
